﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
    using System.Windows;
    using System.ComponentModel;
    using System.Windows.Data;
using System.Windows.Input;
    using MoneyManagerNET.Presentation.ViewModel.Base;

    public class CreateAccountViewModel:ViewModelBase
    {
        public enum AccountType {CheckSave,Investment }
        public CreateAccountViewModel()
        {
            PossibleTypes = new CollectionView(Enum.GetValues(typeof(AccountType)));
            Step=0;

            MoveBackCommand = new Infrastructure.DelegateCommand(o=>MoveBack());
            MoveNextCommand = new Infrastructure.DelegateCommand(o=>MoveNext());
            FinishCommand = new Infrastructure.DelegateCommand(o=>Finish());
            CancelCommand = new Infrastructure.DelegateCommand(o=>Cancel());
        }


        ICollectionView _PossibleTypes;
        public ICollectionView PossibleTypes { get { return _PossibleTypes; } set { _PossibleTypes = value; this.OnChange(()=>PossibleTypes); } }

        public AccountType? SelectedType {
            get {
                var curr = PossibleTypes.CurrentItem;
                if (curr == null) return null;
                else return (AccountType)curr;
            }
        }

        public ICommand MoveBackCommand { get; protected set; }
        public void MoveBack()
        {
            Step--;
        }

        public ICommand MoveNextCommand { get; protected set; }

        public void MoveNext()
        {
            Step++;
        }

        public ICommand  FinishCommand { get; protected set; }
        public void Finish()
        {
            DialogResult = true;
        }

        public ICommand CancelCommand { get; protected set; }
        public void Cancel()
        {
            DialogResult = false; 
        }

        bool? _dialogResult;
        public bool? DialogResult 
        { get { return _dialogResult; } set { _dialogResult = value; this.OnChange(()=>DialogResult); } }

        int _step;
        public int Step
        { 
            get { return _step; } 
            protected set {
                _step = value;

                UpdatePossibleNavigation();

                OnChange("Step"); 
            } 
        }

        private void UpdatePossibleNavigation()
        {
            CanBack = Step != 0;
            CanNext = Step != 2;
            CanFinish = Step == 2;
            CanCancel = true;
        }

        bool _CanNext;
        public bool CanNext { get { return _CanNext; } set { _CanNext = value; this.OnChange(() => CanNext); } }

        bool _CanFinish;
        public bool CanFinish { get { return _CanFinish; } set { _CanFinish = value; this.OnChange(() => CanFinish); } }

        bool _CanCancel;
        public bool CanCancel { get { return _CanCancel; } set { _CanCancel = value; this.OnChange(() => CanCancel); } }

        bool _CanBack;
        public bool CanBack { get { return _CanBack; } set { _CanBack = value; this.OnChange(() => CanBack); } }




        string _AccountName;
        public string AccountName { get { return _AccountName; } set { _AccountName = value; this.OnChange(() => AccountName); } }

        public IAccount CreateAccount()
        {
            IAccount acc = null;
            switch (this.SelectedType.Value)
            {
                case AccountType.CheckSave:
                    acc = App.Storage.New<IBankAccount>();
                    break;
                case AccountType.Investment:
                    acc = App.Storage.New<IStockAccount>();
                    break;
                default:
                    break;
            }
             
            acc.Name = AccountName ;
            return  acc;
        }
    }
}
